package com.matriq.cloudmanager;

import com.matriq.cloudmanager.R;
import com.matriq.cloudmanager.entity.Notice;
import com.matriq.cloudmanager.service.AccountService;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import net.lgpage.activity.BaseActivity;
import net.lgpage.http.HttpMsg;
import net.lgpage.http.SwipeRefeshAsyncTask;
import net.lgpage.util.Checker.StringUtils;
import net.lgpage.util.Cleaner;

public class NoticeDetailActivity extends BaseActivity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_train_note_detail);
		setViews();
		setEvents();
		loadData();
	}

	TextView tv_content;
	android.support.v4.widget.SwipeRefreshLayout srl;
	ScrollView sv;
	TextView tv_title;
	LinearLayout ll_content;
	ImageButton ll_back;
	LinearLayout ll_nothing;
	TextView tv_nothing;

	private void setViews() {
		tv_content = (TextView) this.findViewById(R.id.tv_content);
		srl = (android.support.v4.widget.SwipeRefreshLayout) this.findViewById(R.id.srl);
		sv = (ScrollView) this.findViewById(R.id.sv);
		tv_title = (TextView) this.findViewById(R.id.tv_title);
		ll_content = (LinearLayout) this.findViewById(R.id.ll_content);
		ll_back = (ImageButton) this.findViewById(R.id.ll_back);
		ll_nothing = (LinearLayout) this.findViewById(R.id.ll_nothing);
		tv_nothing = (TextView) this.findViewById(R.id.tv_nothing);
	}

	private void setEvents() {
		ll_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		srl.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				srl.setRefreshing(false);
			}
		});
		setNotingEvents();
	}

	String args[] = { "校院概况", "规章制度" };

	public void setData(String obj) {
		tv_content.setText(Html.fromHtml(obj));
	}


	Notice notice;
	private void loadData() {
		tv_title.setText("通知公告");
		notice = (Notice) intentGetObj(Notice.class);
		reloadObj();
	}

	ObjTask objTask;

	public void reloadObj() {
		if (objTask != null) {
			objTask.cancel(true);
		}
		objTask = new ObjTask(getActivity());
		objTask.setSwipeLayout(srl);
		objTask.execute();
	}

	private class ObjTask extends SwipeRefeshAsyncTask<Object> {

		public ObjTask(Context context) {
			super(context);
		}

		@Override
		public Object doInBackgroundX(String... params) throws Exception {
			return AccountService.getService().noticecontent(notice.getNoticeName());
		}

		@Override
		public void onPostExecuteX(Object result) {
			if (result == null) {
				return;
			} else {
				obj = (HttpMsg) result;
				checkListNoting();
				setData(obj.getData().toString());
			}
		}
	}
	HttpMsg obj;
	private void setNotingEvents(){
		ll_nothing.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				changeListState(false);
				reloadObj();
			}
		});
	}
	
	private void checkListNoting(){
		if(obj == null||StringUtils.isEmpty(obj.getData().toString())){
			changeListState(true);
		}else{
			changeListState(false);
		}
	}
	
	private void changeListState(boolean isnothing){
		if(!isnothing){
			ll_nothing.setVisibility(View.GONE);
			srl.setVisibility(View.VISIBLE);
		}else{
			ll_nothing.setVisibility(View.VISIBLE);
			srl.setVisibility(View.GONE);
		}
	}

	@Override
	public void onDestroy() {
		Cleaner.cleanTask(objTask);
		super.onDestroy();
	}

}